package com.qy.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import com.qy.dao.CheckStationDao;
import java.util.List;
import java.util.Map;
import java.sql.Types;
import com.qy.db.DbParameter;
import java.util.HashMap;
import java.util.ArrayList;
import com.qy.db.pool.DBPoolMgr;
import com.qy.bean.Option;
import com.qy.bean.CheckStation;
import com.qy.db.BaseDao;

/**
* s_check_station daoImpl实现类的生成
* @author zhangwenlong
* @date Sat Feb 10 10:00:35 GMT+08:00 2018
*/ 
public class CheckStationDaoImpl extends BaseDao  implements CheckStationDao {

	@Override
	public boolean createCheckStation(CheckStation checkStation) {
		boolean result = false;
		int id = -1;
		if (checkStation.beginAdd()) {
			String sql = "insert into s_check_station (id,name,point,location,server_id) values (?,?,?,?,?);";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.INTEGER, checkStation.getId()));
			param.put(2, new DbParameter(Types.VARCHAR, checkStation.getName()));
			param.put(3, new DbParameter(Types.VARCHAR, checkStation.getPoint()));
			param.put(4, new DbParameter(Types.VARCHAR, checkStation.getLocation()));
			param.put(5, new DbParameter(Types.INTEGER, checkStation.getServerId()));
			id = execLastId(sql, param);
			result = id > -1;
			checkStation.commitAdd(result);
			checkStation.setId(id);
		}
		return result;
	}

	@Override
	public List<CheckStation> getCheckStationList() {
		String sql = "select * from s_check_station;";
		PreparedStatement pstmt = execQuery(sql, null);
		ResultSet rs = null;
		List<CheckStation> infos = null;
		if (pstmt != null) {
			infos = new ArrayList<CheckStation>();
			try {
				rs = pstmt.executeQuery();
				while (rs.next()) {
					infos.add(resultTo(rs));
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return infos;
	}

	@Override
	public CheckStation getCheckStationById(int id) {
		String sql = "select * from s_check_station where id=?;";
		Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
		param.put(1, new DbParameter(Types.INTEGER, id));
		PreparedStatement pstmt = execQuery(sql, param);
		ResultSet rs = null;
		CheckStation checkStation = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if(rs.next()){
					checkStation = resultTo(rs);
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return checkStation;
	}

	@Override
	public boolean updateCheckStation(CheckStation checkStation) {
		boolean result = false;
		if (checkStation.beginUpdate()) {
			String sql = "update s_check_station set name=?,point=?,location=?,server_id=? where id=?;";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.VARCHAR, checkStation.getName()));
			param.put(2, new DbParameter(Types.VARCHAR, checkStation.getPoint()));
			param.put(3, new DbParameter(Types.VARCHAR, checkStation.getLocation()));
			param.put(4, new DbParameter(Types.INTEGER, checkStation.getServerId()));
			param.put(5, new DbParameter(Types.INTEGER, checkStation.getId()));
			result = execNoneQuery(sql, param) > -1;
			checkStation.commitUpdate(result);
		}
		return result;
	}

	public CheckStation resultTo(ResultSet rs) throws SQLException {
		CheckStation checkStation = new CheckStation();
		checkStation.setId(rs.getInt("id"));
		checkStation.setName(rs.getString("name"));
		checkStation.setPoint(rs.getString("point"));
		checkStation.setLocation(rs.getString("location"));
		checkStation.setServerId(rs.getInt("server_id"));
		checkStation.setOp(Option.None);
		return checkStation;
	}

	@Override
	public Connection openConn(){
		return DBPoolMgr.getStrategyConn();
	}

}

